package album

import (
	"blog/app/common"
	"blog/app/model/album"
	fileModel "blog/app/model/file"
	"blog/app/model/pixiv"
	albumRequest "blog/app/request/album"
	"blog/app/services/system"
	"blog/core"
	"errors"
)

type Album struct {
}

// GetLists , 相册列表
func (rev *Album) GetList(param *albumRequest.Lists) ([]album.ListsAlbum, int64) {
	var lists []album.ListsAlbum
	var count int64
	core.DbConnect.Model(system.Model.AlbumModel).Select("(?) as count,id,name,is_recommend,cover", core.DbConnect.Model(system.Model.AlbumImagesModel).Where(system.Model.AlbumImagesModel.TableName()+".aid = "+system.Model.AlbumModel.TableName()+".id").Select("count(*)")).Count(&count).Offset((param.Page - 1) * param.Limit).Limit(param.Limit).Find(&lists)
	hash := make([]string, 0)
	for _, v := range lists {
		hash = append(hash, v.Cover)
	}

	hashLists := common.Cache.Files(hash)
	for i := 0; i < len(lists); i++ {
		if _, ok := hashLists[lists[i].Cover]; ok {
			lists[i].Cover = hashLists[lists[i].Cover]
		} else {
			lists[i].Cover = ""
		}
	}

	return lists, count
}

func (rev *Album) GetInfo() (*album.Album, error) {
	var info *album.Album
	return info, core.DbConnect.Model(system.Model.AlbumModel).Find(&info).Error
}

// Create , 添加相册
func (rev *Album) Create(param *albumRequest.Create) error {
	var info album.Album

	if param.Cover == "" {
		var offset int
		var infoPixiv pixiv.Pixiv
		core.DbConnect.Model(system.Model.PixivModel).Select("FLOOR(RAND() * COUNT(*)) AS `offset`").Find(&offset)
		core.DbConnect.Model(system.Model.PixivModel).Offset(offset).Limit(1).Select("url").Find(&infoPixiv)
		info.Cover = infoPixiv.Url
	}

	info.Name = param.Name
	info.Describe = param.Describe
	return core.DbConnect.Model(system.Model.AlbumModel).Create(&info).Error
}

// Edit , 相册编辑
func (rev Album) Edit(id int, param *album.EditReqAlbum) error {
	var info album.Album
	if core.DbConnect.Model(system.Model.AlbumModel).Find(&info, id).RowsAffected == 0 {
		return errors.New("相册不存在")
	}
	info.Name = param.Name
	info.Cover = param.Path
	return core.DbConnect.Model(system.Model.AlbumModel).Where("id = ?", info.ID).Updates(info).Error
}

func (rev *Album) Del(id int) error {
	if id == 0 {
		return errors.New("必须指定相册")
	}

	if core.DbConnect.Model(system.Model.AlbumModel).Where("id = ?", id).RowsAffected == 0 {
		return errors.New("相册不存在")
	}

	return nil
	//return core.DbConnect.Delete(&album.Album{ID: id}).Error
}

func (rev *Album) Upload(id int, path string) error {
	var count int64
	var fileInfo fileModel.File

	if core.DbConnect.Model(system.Model.AlbumModel).Where("id = ?", id).Count(&count); count == 0 {
		return errors.New("相册不存在")
	}

	if core.DbConnect.Model(system.Model.FileModel).Where("hash = ?", path).Find(&fileInfo).RowsAffected == 0 {
		return errors.New("相关文件不存在")
	}

	if core.DbConnect.Model(system.Model.AlbumImagesModel).Where("url = ?", fileInfo.Hash).Count(&count); count >= 1 {
		return nil
	}

	err := core.DbConnect.Model(system.Model.AlbumImagesModel).Create(&album.ImagesAlbum{Aid: id, Url: fileInfo.Hash}).Error
	if err != nil {
		return err
	}

	core.DbConnect.Model(system.Model.AlbumModel).Where("id = ?", id).Update("cover", fileInfo.Hash)
	return nil
}
